import { request } from '@@/plugin-request/request';

export async function getStockByPage(body: StockQueryDTO) {
  return request<BaseResponse<IPage<StockVO>>>('/api/stock/list', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
  });
}
export async function downloadBelowSafeLevelStock() {
  try {
    // 假设接口返回二进制文件流
    const response = await request('/api/stock/exportBelowSafeLevel', {
      method: 'GET',
      responseType: 'blob',
    });

    // 创建一个临时的 URL 对象，指定正确的 MIME 类型
    const url = window.URL.createObjectURL(
      new Blob([response], {
        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      }),
    );

    // 创建一个 a 标签
    const link = document.createElement('a');
    link.href = url;
    // 设置下载文件名
    link.setAttribute('download', '库存告急缺货文件.xlsx');

    document.body.appendChild(link);
    // 模拟点击下载
    link.click();
    // 移除 a 标签
    document.body.removeChild(link);
    // 释放 URL 对象
    window.URL.revokeObjectURL(url);
  } catch (error) {
    console.error('下载失败:', error);
  }
}

export async function updateSafeStock(body: {warehouseId: string,goodsId: string, safeStock: number}) {
  return request<BaseResponse<IPage<StockVO>>>('/api/stock/updateSafeStock', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
  });
}
